package com.woniuxy.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

/**
 * @Author: ideaAdmin
 * TODO:文件上传控制器
 * Date: 2022/2/9:13:26
 * Description:
 */
@Controller
@RequestMapping("/file")
public class FileController {


    @RequestMapping("/uploadOne")
    //单个文件上传
    public String fileUploadOne(HttpServletRequest request, HttpServletResponse response,
                                String des, MultipartFile upload, Model model) {
        if (!upload.isEmpty()) {
            System.out.println("文件描述" + des);

            //获得上传文件的文件名
            String filename = upload.getOriginalFilename();

            //获得 /files/ 当前文件夹所在的绝对路径
            //目录在out下
            String realPath = request.getServletContext().getRealPath("/files/");

            File file = new File(realPath, filename);

            //判断当前文件的父文件夹是否存在
            if (!file.getParentFile().exists()) {
                //不存在就创建
                file.getParentFile().mkdir();
            }

            try {
                //文件上传到哪里
                upload.transferTo(new File(realPath + File.separator + filename));

                model.addAttribute("msg", "文件上传成功");
            } catch (IOException e) {
                e.printStackTrace();
                model.addAttribute("msg", "文件上传失败" + e.getMessage());
            }

        }
        return "ok";
    }

    @RequestMapping("/upload")
    //多个不同名字的文件上传接收
    public String fileUpload(String username, MultipartFile upload1, MultipartFile upload2) throws IOException {
        System.out.println(username);
        //获得上传文件的名字
        String filename = upload1.getOriginalFilename();
        upload1.transferTo(new File("Z:\\7Z\\" + filename));

        String filename2 = upload2.getOriginalFilename();
        upload2.transferTo(new File("Z:\\7Z\\" + filename2));
        return "index";
    }

    @RequestMapping("/uploads")
    //多个同名字的文件上传接收 接收的参数名必须一致
    public String fileUpload(HttpServletRequest request, HttpServletResponse response,
                             String username, MultipartFile[] upload) throws IOException {
        System.out.println(username);

        for (MultipartFile uploadFile : upload) {
            //获得上传文件的名字
            String filename = uploadFile.getOriginalFilename();
            uploadFile.transferTo(new File("Z:\\7Z\\" + filename));
        }

        return "index";
    }

    //下载文件
    @RequestMapping("/download")
    public ResponseEntity<byte[]> fileDow(HttpServletRequest request,
                                          @RequestParam("filename") String filename, Model model) {
        //获取下载文件路径
        String path = request.getServletContext().getRealPath("/files/");

        //设置http的头
        HttpHeaders httpHeaders = new HttpHeaders();

        //拼接获得文件所在的位置
        File file = new File(path + File.separator + filename);

        //下载显示的文件名,解决中文名称乱码的问题
        String downloadFilename = new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);


        //通知浏览器以attachment(下载方式)打开图片
        httpHeaders.setContentDispositionFormData("attacchment", downloadFilename);
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        try {
            //发送一个ResponseEntity 返回
            return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), httpHeaders, HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
